import 'package:flutter/material.dart';

import '../../config/global_config.dart';
import '../../utils/screenutil/fit.dart';

class Dialogs {
  /// 单提示对话框
  /// * [context] 上下文
  /// * [title] String|选填|标题文案
  /// * [content] String|选填|主体文案
  /// * [confirm] Function|选填|函数
  /// * [confirmTxt] String|选填|按钮文字|默认：确定
  static alertDialog(
    BuildContext context, {
    String title = "提示",
    String content = "提示的内容",
    Function confirm,
    String confirmTxt = "确定",
  }) {
    _showDialog(
      context,
      AlertDialog(
        title: Text(title, style: TextStyle(fontSize: 28.f)),
        content: SingleChildScrollView(
          child: ListBody(
            children: [Text("$content", style: TextStyle(fontSize: 24.f))],
          ),
        ),
        actions: [
          TextButton(
            child: Text(confirmTxt),
            onPressed: () {
              Navigator.of(context).pop();
              if (confirm != null) confirm();
            },
          )
        ],
      ),
    );
  }

  /// 确认对话框
  /// * [context] 上下文
  /// * [title] String|选填|标题文案
  /// * [content] String|选填|主体文案
  /// * [confirm] Function|选填|确认回调函数
  /// * [confirmTxt] String|选填|按钮文字|默认：确定
  /// * [cancel] Function|选填|取消回调函数
  /// * [cancelTxt] String|选填|按钮文字|默认：取消
  static confirmDialog(
    BuildContext context, {
    String title = "提示",
    String content = "提示的内容",
    Function confirm,
    String confirmTxt = "确定",
    Function cancel,
    String cancelTxt = "取消",
  }) {
    _showDialog(
      context,
      AlertDialog(
        title: Text(title, style: TextStyle(fontSize: 28.f)),
        content: SingleChildScrollView(
          child: ListBody(
            children: [Text("$content", style: TextStyle(fontSize: 24.f))],
          ),
        ),
        actions: [
          FlatButton(
            child: Text(cancelTxt, style: TextStyle(color: GlobalConfig.theme)),
            onPressed: () {
              Navigator.of(context).pop();
              if (cancel != null) cancel();
            },
          ),
          FlatButton(
            child:
                Text(confirmTxt, style: TextStyle(color: GlobalConfig.theme)),
            onPressed: () {
              Navigator.of(context).pop();
              if (confirm != null) confirm();
            },
          )
        ],
      ),
    );
  }

  // 弹窗基础架子
  // * [context] BuildContext|必填
  // * [child] widget|必填
  static _showDialog(BuildContext context, child) {
    showDialog(context: context, builder: (BuildContext context) => child);
  }
}
